# -------------------------------------01 加载MNIST数据集-----------------------
from keras.datasets import mnist

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# -------------------------------------02 模型定义-----------------------
from keras import models
from keras import layers
from keras import regularizers

model = models.Sequential()
model.add(layers.Conv2D(64, (3, 3), kernel_regularizer=regularizers.l2(0.001),
                        activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D(2, 2))
model.add(layers.Conv2D(128, (3, 3), kernel_regularizer=regularizers.l2(0.001), activation='relu'))
model.add(layers.MaxPooling2D(2, 2))
model.add(layers.Conv2D(256, (3, 3), kernel_regularizer=regularizers.l2(0.001), activation='relu'))
# 重要的是，卷积神经网络接收形状为 (image_height, image_width, image_channels)
# 的输入张量（不包括批量维度）。本例中设置卷积神经网络处理大小为 (28, 28, 1) 的输入张量，
# 这正是 MNIST 图像的格式。我们向第一层传入参数 input_shape=(28, 28, 1) 来完成此设置。
# 我们来看一下目前卷积神经网络的架构。
# print(model.summary())
# _________________________________________________________________
# Layer (type)                 Output Shape              Param #
# =================================================================
# conv2d_1 (Conv2D)            (None, 26, 26, 32)        320
# _________________________________________________________________
# max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32)        0
# _________________________________________________________________
# conv2d_2 (Conv2D)            (None, 11, 11, 64)        18496
# _________________________________________________________________
# max_pooling2d_2 (MaxPooling2 (None, 5, 5, 64)          0
# _________________________________________________________________
# conv2d_3 (Conv2D)            (None, 3, 3, 64)          36928
# =================================================================
# Total params: 55,744
# Trainable params: 55,744
# Non-trainable params: 0

# 可以看到，每个 Conv2D 层和 MaxPooling2D 层的输出都是一个形状为 (height, width,
# channels) 的 3D 张量。宽度和高度两个维度的尺寸通常会随着网络加深而变小。通道数量由传
# 入 Conv2D 层的第一个参数所控制（32 或 64）。

# 下一步是将最后的输出张量［大小为 (3, 3, 64)］输入到一个密集连接分类器网络中，
# 即 Dense 层的堆叠，你已经很熟悉了。这些分类器可以处理 1D 向量，而当前的输出是 3D 张量。
# 首先，我们需要将 3D 输出展平为 1D，然后在上面添加几个 Dense 层。

# -------------------------------------03 在卷积神经网络上添加分类器-----------------------
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
# print(model.summary())
# _________________________________________________________________
# Layer (type)                 Output Shape              Param #
# =================================================================
# conv2d_1 (Conv2D)            (None, 26, 26, 32)        320
# _________________________________________________________________
# max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32)        0
# _________________________________________________________________
# conv2d_2 (Conv2D)            (None, 11, 11, 64)        18496
# _________________________________________________________________
# max_pooling2d_2 (MaxPooling2 (None, 5, 5, 64)          0
# _________________________________________________________________
# conv2d_3 (Conv2D)            (None, 3, 3, 64)          36928
# _________________________________________________________________
# flatten_1 (Flatten)          (None, 576)               0
# _________________________________________________________________
# dense_1 (Dense)              (None, 64)                36928
# _________________________________________________________________
# dense_2 (Dense)              (None, 10)                650
# =================================================================
# Total params: 93,322
# Trainable params: 93,322
# Non-trainable params: 0
# _________________________________________________________________
# None

# 如你所见，在进入两个 Dense 层之前，形状 (3, 3, 64) 的输出被展平为形状 (576,) 的
# 向量。

# -------------------------------------04 准备数据-----------------------
from keras.utils import to_categorical

# 注意这里的train_images.reshape的形状是(28,28,1)了，因为这里的convnet层根之前的dense层的输入不一样
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# -------------------------------------05 编译模型-----------------------
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# -------------------------------------06 训练模型-----------------------
model.fit(train_images, train_labels, epochs=5, batch_size=64)

# -------------------------------------07 评估模型-----------------------
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('test_loss:', test_loss)
print('test_acc:', test_acc)
# test_loss: 0.0392793850958
# test_acc: 0.9903